import { createAction, handleActions } from 'redux-actions'
import { CALL_API } from 'redux-api-middleware'

import getApiPath from '../../helpers/getApiPath'
import getFetchOptions from '../../helpers/getFetchOptions'
import getQuery from '../../helpers/getQuery'

// ------------------------------------
// Constants (Action Types)
// ------------------------------------

// pending request
const FETCH_REQUEST = Symbol('FETCH_REQUEST')
const FETCH_FAILURE = Symbol('FETCH_FAILURE')
const FETCH_LIST_SUCCESS = Symbol('FETCH_LIST_SUCCESS')

// ------------------------------------
// Actions (Action Creator)
// ------------------------------------


export const saveComment = (params, callback) => {

  const fetchOptions = getFetchOptions(getApiPath('CustomerOrder/OrderEvaluate?type=1'), 'POST', {
    body: JSON.stringify(params)
  })

  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        FETCH_REQUEST,
        {
          type: Symbol('SAVE_COMMENT_SUCCESS'),
          payload: (action, state, json) => json.data
        },
        FETCH_FAILURE
      ]
    }
  }
}

export const fetchComment = (params, callback) => {

  const fetchOptions = getFetchOptions(getApiPath('CustomerOrder/GetOrderEvaluateByOrderId', params))

  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        FETCH_REQUEST,
        {
          type: Symbol('FETCH_COMMENT_SUCCESS'),
          payload: (action, state, json) => json.data
        },
        FETCH_FAILURE
      ]
    }
  }
}

export const fetchList = (params, callback) => {
  
  const fetchOptions = getFetchOptions(getApiPath('CustomerOrder/GetOrderTruckPage', params))

  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        FETCH_REQUEST,
        {
          type: FETCH_LIST_SUCCESS,
          payload: (action, state, json) => json.Data
        },
        FETCH_FAILURE
      ]
    }
  }
}

export const fetchPosition = (params, callback) => {
  
  const fetchOptions = getFetchOptions(getApiPath('CarPosition', params))

  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        FETCH_REQUEST,
        {
          type: Symbol('FETCH_POSITION_SUCCESS'),
          payload: (action, state, json) => json.Data
        },
        FETCH_FAILURE
      ]
    }
  }
}

export const fetchBaseCfg = (params,callback) => {
  const fetchOptions = getFetchOptions(getApiPath('DataSource/GetConfigByCode', params))
  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        Symbol('FETCH_REQUEST'),
        {
          type: Symbol('FETCH_BASE_CONFIG_SUCCESS'),
          payload: (action, state, json) => json
        },
        Symbol('FETCH_FAILURE')
      ]
    }
  }
}


/**
 * 暴露actions到外面，方便使用react-redux connect绑定到Container Component
 */
export const actions = {
  saveComment, // 保存地址
  fetchComment,
  fetchList,
  fetchPosition,
  fetchBaseCfg
}

// ------------------------------------
// Reducers
// ------------------------------------
const initialState = {
  pending: false,

  list: [],
  pageIndex: 0,
  pageCount: 0
}

export default handleActions({
  
  [FETCH_REQUEST](state) {
    return {
      ...state,
      pending: true
    }
  },

  [FETCH_FAILURE](state) {
    return {
      ...state,
      pending: false
    }
  },

  [FETCH_LIST_SUCCESS](state, {payload}) {
    let list = payload.PageIndex == 1 ? payload.Data : state.list.concat(payload.Data)
    return {
      ...state,
      pending: false,
      list: list,
      pageCount: +payload.PageCount,
      pageIndex: +payload.PageIndex
    }
  }

}, initialState)
